#include "Player.h"
#include <stdio.h>
Player::Player()
{
}
void Player::Initialize()
{
    // initialize player
    X = 100;
    Y = 365;
    velX = 0;
    velY = 0;
    flipped = false;
    speed = 0.5;
    initAnimations();
    currentAnimation = &anims[ANIMATION_STANDING];
    anims[ANIMATION_STANDING].play();
    anims[ANIMATION_RUNNING].play();
    isJumping = false;
    inAir = false;
}
void Player::initAnimations()
{
    anims[ANIMATION_STANDING].LoadFrames("media/sprite_standing.bmp", 75, 111, 2);
    anims[ANIMATION_STANDING].animationSpeed = 5;
    anims[ANIMATION_RUNNING].LoadFrames("media/sprite_running.bmp", 75, 111, 16);
    anims[ANIMATION_RUNNING].animationSpeed = 50*speed;
    anims[ANIMATION_JUMPING].LoadFrames("media/sprite_jump.bmp", 75, 111, 5);
    anims[ANIMATION_JUMPING].animationSpeed = 30;
}
void Player::startJump()
{
    isJumping = true;
    anims[ANIMATION_JUMPING].play();
}
void Player::updateJump()
{
    if(!anims[ANIMATION_JUMPING].finished)
        anims[ANIMATION_JUMPING].update();
    else
    {
        currentAnimation = &anims[ANIMATION_STANDING];
        isJumping = false;
        inAir = true;
        velY = -4;
    }
}
void Player::update()
{
    if(isJumping)
    {
        updateJump();
    }
    else if(key[KEY_W] && !inAir)
    {
        currentAnimation = &anims[ANIMATION_JUMPING];
        startJump();
    }


    if(key[KEY_D])
    {
        currentAnimation = &anims[ANIMATION_RUNNING];
        flipped = false;
        X+=speed;
    }
    else if(key[KEY_A])
    {
        currentAnimation = &anims[ANIMATION_RUNNING];
        X-=speed;
        flipped = true;
    }
    else if(!isJumping)
    {
        currentAnimation = &anims[ANIMATION_STANDING];
    }


    //update position
    velY += 0.1;
    X+=velX;
    Y+=velY;
    if(Y > 365)
    {
        Y = 365;
        velY = 0;
        inAir = false;
    }
    currentAnimation->update();
}
